package com.qfedu.controller;

import com.qfedu.common.JsonResult;
import com.qfedu.pojo.User;
import com.qfedu.service.UserService;
import com.qfedu.util.JsonUtils;
import com.qfedu.util.StrUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : ang
 * @Program: oasys
 * @Title : UserController
 * @Date : 2020-09-01 19:37
 **/
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
    public JsonResult login(String loginname, String password, HttpSession session) {
        JsonResult result = null;
        try {
            User user = userService.findByName(loginname, password);
            session.setAttribute(StrUtils.LOGIN_USER,user);
            result = JsonUtils.createJsonBean(1,null);
        }catch (Exception e) {
            e.printStackTrace();
            result = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return result;
    }
    @RequestMapping("/register.do")
    @ResponseBody
    public JsonResult register(User user){
        JsonResult result = null;
        try {
            userService.addUser(user);
            result = JsonUtils.createJsonBean(1,null);
        }catch (Exception e) {
            result = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return result;
    }

    @RequestMapping("/query.do")
    @ResponseBody
    public JsonResult getLoginInfo (HttpSession session) {
        User user = (User) session.getAttribute(StrUtils.LOGIN_USER);
        User u = userService.findByName(user.getLoginname(), user.getPassword());

        JsonResult jsonResult = null;
        if (u != null) {
            jsonResult = new JsonResult(1, u);
        } else {
            jsonResult = new JsonResult(0, "用户未登录");
        }
        return jsonResult;
    }
    @RequestMapping("/findAll.do")
    @ResponseBody
    public Map<String, Object> findAll(User user, int page, Integer limit) {
        if (user.getUsername() == null) {
            user.setUsername("");
        }
        List<User> all = userService.findAll(user, page, limit);
        long total = ((Page) all).getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", all);
        return map;
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult deleteById(Integer id) {
        JsonResult jsonResult = null;
        try {
            userService.deleteById(id);
            jsonResult = new JsonResult(1, "删除成功");
        } catch (Exception e) {
            jsonResult = new JsonResult(0, e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("/addUser.do")
    @ResponseBody
    public JsonResult addUser(User user) {
        JsonResult jsonResult = null;
        try {
            userService.addUser(user);
            jsonResult = new JsonResult(1, "添加成功");
        } catch (Exception e) {
            jsonResult = new JsonResult(0, e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("/findById.do")
    @ResponseBody
    public JsonResult findById(Integer id) {
        User user = userService.findById(id);
        return new JsonResult(1, user);
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public JsonResult update(Integer id, String password, Integer status) {

        userService.updateById(id, password, status);
        return new JsonResult(1, "gengx");
    }
}
